﻿using Liju.Common;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using System.Drawing.Imaging;
using BLL;

namespace LiJu
{
    public partial class MESControl : Form
    {
        public MESControl()
        {
            InitializeComponent();
        }

        private void MESControl_Load(object sender, EventArgs e)
        {
            //加载初始数据

            //工位、产线
            TbxLine.Text = MES.Line;
            TbxWorkStation.Text = MES.WorkStation;

            //加载异常报修-异常大类下拉框数据
            string errType = MES.GetExceptionMenu1();

            // 将 JSON 字符串转换为 JObject
            JObject jsonObject = JObject.Parse(errType);

            if ((int)jsonObject["ResponseCode"] == 1)
            {
                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)jsonObject["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 ComboBox 中
                foreach (JObject msg in responseMsgArray)
                {
                    string groupName = (string)msg["BYD_ComExceptionGroupName"];
                    CbxErrType.Items.Add(groupName);

                }
            }

            //加载异常确认信息
            LoadErrRepairsConfirmInfo();



        }

        /// <summary>
        /// 异常大类 选中时加载异常明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CbxErrType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //选中的异常大类
            string errTypeValue = this.CbxErrType.Text;
            string errDetail = MES.GetExceptionMenu2(errTypeValue);
            // 将 JSON 字符串转换为 JObject
            JObject jsonObject = JObject.Parse(errDetail);

            if ((int)jsonObject["ResponseCode"] == 1)
            {
                CbxErrDetail.Items.Clear();

                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)jsonObject["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 ComboBox 中
                foreach (JObject msg in responseMsgArray)
                {
                    string groupName = (string)msg["Description"];
                    CbxErrDetail.Items.Add(groupName);

                }
            }

        }

        private void TbxDeviceSN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                //扫码后触发校验
                string DeviceSN = this.TbxDeviceSN.Text;
                string deviceType = MES.GetToolDeviceList(DeviceSN);

                // 将 JSON 字符串转换为 JObject
                JObject jsonObject = JObject.Parse(deviceType);
                if ((int)jsonObject["ResponseCode"] == 1)
                {
                    this.TbxDeviceType.Clear();

                    // 获取 ResponseMsg 数组
                    JArray responseMsgArray = (JArray)jsonObject["ResponseMsg"];

                    this.TbxDeviceType.Text = responseMsgArray[0]["ResourceType"].ToString();

                    this.BtnConfirm.Enabled = true;

                }
                else
                {
                    string msg = (string)jsonObject["ResponseMsg"];
                    MessageBox.Show(msg, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.TbxDeviceSN.Clear();
                    this.TbxDeviceType.Clear();
                    this.TbxDeviceSN.Focus();
                }
            }
        }

        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            string code = this.TbxDeviceSN.Text.Trim();
            string type = this.TbxDeviceType.Text.Trim();
            string work = this.TbxWorkStation.Text.Trim();
            string line = this.TbxLine.Text.Trim();
            string errtype = this.CbxErrType.Text.Trim();
            string errdateil = this.CbxErrDetail.Text.Trim();
            string remark = this.TbxOtherErr.Text.Trim();

            if (string.IsNullOrWhiteSpace(errtype))
            {
                MessageBox.Show("请选择异常大类", "系统提示");
                this.CbxErrType.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(errdateil))
            {
                MessageBox.Show("请选择异常明细", "系统提示");
                this.CbxErrDetail.Focus();
                return;
            }

            string result = MES.SaveExceptionInfo(code, type, errtype, errdateil);

            // 将 JSON 字符串转换为 JObject
            JObject jsonObject = JObject.Parse(result);
            if ((int)jsonObject["ResponseCode"] == 1)
            {
                Bll.InsertErrRecordData(code, type, work, line, errtype, errdateil, remark);
            }

            string msg = (string)jsonObject["ResponseMsg"];
            MessageBox.Show(msg, "系统提示");

        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            this.TbxDeviceSN.Clear();
            this.TbxDeviceType.Clear();
            this.TbxOtherErr.Clear();
            this.TbxDeviceSN.Focus();
            this.BtnConfirm.Enabled = false;
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {

            //加载异常确认信息
            LoadErrRepairsConfirmInfo();
        }

        /// <summary>
        /// 加载异常确认信息
        /// </summary>
        public void LoadErrRepairsConfirmInfo()
        {
            //加载异常确认信息
            string repairsList = MES.GetNeedConfirmRepairsList();
            // 将 JSON 字符串转换为 JObject
            JObject objrepairsList = JObject.Parse(repairsList);

            if ((int)objrepairsList["ResponseCode"] == 1)
            {

                DgvErrConfirm.Rows.Clear();
                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)objrepairsList["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 DataGridView 中
                foreach (JObject msg in responseMsgArray)
                {
                    int index = DgvErrConfirm.Rows.Add();
                    DgvErrConfirm.Rows[index].Cells[0].Value = (int)msg["SequenceNo"];
                    DgvErrConfirm.Rows[index].Cells[1].Value = (string)msg["ResourceCode"];
                    DgvErrConfirm.Rows[index].Cells[2].Value = (string)msg["ResourceName"];
                    DgvErrConfirm.Rows[index].Cells[3].Value = (string)msg["CommonException"];
                    DgvErrConfirm.Rows[index].Cells[4].Value = (string)msg["RepairPerson"];
                    DgvErrConfirm.Rows[index].Cells[5].Value = (string)msg["RepairDate"];
                    DgvErrConfirm.Rows[index].Cells[6].Value = (string)msg["WorkStation"];
                    DgvErrConfirm.Rows[index].Cells[7].Value = (string)msg["ResourceType"];
                    DgvErrConfirm.Rows[index].Cells[8].Value = (string)msg["BYD_ToolReworkID"];
                    DgvErrConfirm.Rows[index].Cells[9].Value = (string)msg["BYD_ToolRepairID"];


                }
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Tbx2Code_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                //扫码后触发校验
                string DeviceSN = this.Tbx2Code.Text;
                string productList = MES.GetSongJianProductInfo(DeviceSN);

                // 将 JSON 字符串转换为 JObject
                JObject jsonObject = JObject.Parse(productList);
                if ((int)jsonObject["ResponseCode"] == 1)
                {
                    ClearPordRework();

                    // 获取 ResponseMsg 数组
                    JArray responseMsgArray = (JArray)jsonObject["ResponseMsg"];

                    this.Tbx2MfgOrderName.Text = responseMsgArray[0]["MfgOrderName"].ToString();
                    this.Tbx2MfgOrderStartDate.Text = responseMsgArray[0]["MfgOrderStartDate"].ToString();
                    this.Tbx2MfgOrderQty.Text = responseMsgArray[0]["MfgOrderQty"].ToString();
                    this.Tbx2ProductName.Text = responseMsgArray[0]["ProductName"].ToString();
                    this.Tbx2ProductCode.Text = responseMsgArray[0]["ProductCode"].ToString();
                    this.Tbx2CurStation.Text = responseMsgArray[0]["CurStation"].ToString();
                    this.Tbx2Reworkflow.Text = responseMsgArray[0]["Reworkflow"].ToString();
                    this.Tbx2ReworkStep.Text = responseMsgArray[0]["ReworkStep"].ToString();

                    //根据送检工序ID获取送检原因
                    string WorkflowStepId = responseMsgArray[0]["WorkflowStepId"].ToString();
                    this.Tbx2WorkflowStepId.Text = WorkflowStepId;

                    string reasonList = MES.GetSongJianReasonList(WorkflowStepId);
                    JObject reas = JObject.Parse(reasonList);
                    if ((int)reas["ResponseCode"] == 1)
                    {
                        this.Cbx2Reason.Items.Clear();

                        // 获取 ResponseMsg 数组
                        JArray reasArray = (JArray)reas["ResponseMsg"];
                        // 遍历数组中的每个元素，并添加到 ComboBox 中
                        foreach (JObject msg in reasArray)
                        {
                            string ReworkReasonName = (string)msg["ReworkReasonName"];
                            this.Cbx2Reason.Items.Add(ReworkReasonName);

                        }
                    }



                    this.Btn2Confirm.Enabled = true;

                }
                else
                {
                    string msg = (string)jsonObject["ResponseMsg"];

                    this.Tbx2Code.Clear();
                    //清空文本框内容
                    ClearPordRework();

                    MessageBox.Show(msg, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        /// <summary>
        /// 清空送检文本框内容
        /// </summary>
        public void ClearPordRework()
        {

            this.Tbx2MfgOrderName.Clear();
            this.Tbx2MfgOrderStartDate.Clear();
            this.Tbx2MfgOrderQty.Clear();
            this.Tbx2ProductName.Clear();
            this.Tbx2ProductCode.Clear();
            this.Tbx2CurStation.Clear();
            this.Tbx2Reworkflow.Clear();
            this.Tbx2ReworkStep.Clear();
            this.Tbx2Remark.Clear();
            this.Tbx2WorkflowStepId.Clear();
            this.Cbx2Reason.Items.Clear();
            this.Cbx2Reason.Text = "";
            this.Tbx2Code.Focus();
            this.Btn2Confirm.Enabled = false;
        }

        private void Btn2Reset_Click(object sender, EventArgs e)
        {
            this.Tbx2Code.Clear();
            this.Btn2Confirm.Enabled = false;
            ClearPordRework();
        }

        private void Btn2Confirm_Click(object sender, EventArgs e)
        {

            string code = this.Tbx2Code.Text.Trim();
            string orderName = this.Tbx2MfgOrderName.Text.Trim();
            string orderDate = this.Tbx2MfgOrderStartDate.Text.Trim();
            int qty = Convert.ToInt32(this.Tbx2MfgOrderQty.Text.Trim());
            string productName = this.Tbx2ProductName.Text.Trim();
            string productCode = this.Tbx2ProductCode.Text.Trim();
            string curstation = this.Tbx2CurStation.Text.Trim();
            string reworkflow = this.Tbx2Reworkflow.Text.Trim();
            string reworkstep = this.Tbx2ReworkStep.Text.Trim();
            string workId = this.Tbx2WorkflowStepId.Text.Trim();
            string remark = this.Tbx2Remark.Text.Trim();
            string reason = this.Cbx2Reason.Text.Trim();

            if (string.IsNullOrWhiteSpace(reason))
            {

                MessageBox.Show("请选择送检原因", "系统提示");
                this.Cbx2Reason.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(remark))
            {

                MessageBox.Show("请输入送检备注", "系统提示");
                this.Tbx2Remark.Focus();
                return;
            }


            string result = MES.SaveSongJianInfo(code, reason, remark, reworkflow, workId, productCode);
            // 将 JSON 字符串转换为 JObject
            JObject jsonObject = JObject.Parse(result);
            if ((int)jsonObject["ResponseCode"] == 1)
            {
                Bll.InsertProductInspectionData(code, orderName, orderDate, qty, productCode, productName, curstation, reworkflow, reworkstep, workId, remark, reason);
            }

            string msg = (string)jsonObject["ResponseMsg"];
            MessageBox.Show(msg, "系统提示");

            this.Tbx2Code.Clear();
            ClearPordRework();
        }

        private void Cbx3BillNo_Click(object sender, EventArgs e)
        {

            string beginTime = this.DtmBegin.Text;
            string endTime = this.DtmEnd.Text;

            //文件查看-获取线体工单信息
            string errType = MES.GetLineOrderList(beginTime, endTime);

            // 将 JSON 字符串转换为 JObject
            JObject jsonObject = JObject.Parse(errType);


            this.Cbx3BillNo.Text = "";
            this.Cbx3BillNo.Items.Clear();

            if ((int)jsonObject["ResponseCode"] == 1)
            {
                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)jsonObject["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 ComboBox 中
                foreach (JObject msg in responseMsgArray)
                {
                    string groupName = (string)msg["MfgOrderName"];
                    this.Cbx3BillNo.Items.Add(groupName);

                }
                if (responseMsgArray.Count > 0)
                {

                    this.Btn3Query.Enabled = true;
                }
            }
        }

        private void Btn3Query_Click(object sender, EventArgs e)
        {
            string strMfgOrder = this.Cbx3BillNo.Text.Trim();

            if (string.IsNullOrWhiteSpace(strMfgOrder))
            {

                MessageBox.Show("请选择工单", "系统提示");
                this.Cbx3BillNo.Focus();
                return;
            }

            // 文件查看-根据工单获取产品SOP文件信息
            string files = MES.GetSOPByOrder(strMfgOrder);

            // 将 JSON 字符串转换为 JObject
            JObject fileList = JObject.Parse(files);

            if ((int)fileList["ResponseCode"] == 1)
            {

                DgvFile.Rows.Clear();
                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)fileList["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 DataGridView 中
                foreach (JObject msg in responseMsgArray)
                {
                    int index = DgvFile.Rows.Add();
                    DgvFile.Rows[index].Cells[0].Value = (string)msg["SopFileNumber"];
                    DgvFile.Rows[index].Cells[1].Value = (string)msg["SopFileVersion"];
                    //DgvFile.Rows[index].Cells[2].Value = (string)msg["MfgOrderName"];
                    //DgvFile.Rows[index].Cells[3].Value = (string)msg["MfgLineName"];
                    //DgvFile.Rows[index].Cells[4].Value = (string)msg["ProductCode"];
                    //DgvFile.Rows[index].Cells[5].Value = (string)msg["ProductName"];

                }
            }
            else
            {
                string msg = (string)fileList["ResponseMsg"];
                MessageBox.Show(msg, "系统提示");
            }
        }

        private void Btn3Reset_Click(object sender, EventArgs e)
        {

            this.Cbx3BillNo.Text = "";
            this.Cbx3BillNo.Items.Clear();
            this.Btn3Query.Enabled = false;
            DgvFile.Rows.Clear();
        }

        private void Btn4Confirm_Click(object sender, EventArgs e)
        {
            string strToolDeviceCode = this.Tbx4Code.Text.Trim();
            if (string.IsNullOrWhiteSpace(strToolDeviceCode))
            {

                MessageBox.Show("请输入工器具条码", "系统提示");
                this.Tbx4Code.Focus();
                return;
            }

            // 文件查看-根据设备编码获取ESOP文件信息
            string equipment = MES.GetToolDeviceESOP(strToolDeviceCode);

            // 将 JSON 字符串转换为 JObject
            JObject equipmentList = JObject.Parse(equipment);

            if ((int)equipmentList["ResponseCode"] == 1)
            {

                DgvEquipment.Rows.Clear();
                // 获取 ResponseMsg 数组
                JArray responseMsgArray = (JArray)equipmentList["ResponseMsg"];
                // 遍历数组中的每个元素，并添加到 DataGridView 中
                foreach (JObject msg in responseMsgArray)
                {
                    int index = DgvEquipment.Rows.Add();
                    DgvEquipment.Rows[index].Cells[0].Value = (string)msg["ResourceCode"];
                    DgvEquipment.Rows[index].Cells[1].Value = (string)msg["ResourceName"];
                    DgvEquipment.Rows[index].Cells[2].Value = (string)msg["ResourceType"];
                    DgvEquipment.Rows[index].Cells[3].Value = (string)msg["MaintenanceDocument"];
                    DgvEquipment.Rows[index].Cells[4].Value = (string)msg["OperationDocument"];

                }
            }
            else {
                string msg = (string)equipmentList["ResponseMsg"];
                MessageBox.Show(msg, "系统提示");
            }
        }

        private void Btn4Reset_Click(object sender, EventArgs e)
        {

            this.Tbx4Code.Clear();
            DgvEquipment.Rows.Clear();
        }
    }
}
